package company.shopee_F;

import java.util.*;

public class Shopee04 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        //cache
        Map<Integer, Integer> xMap = new HashMap<>();
        Map<Integer, Integer> yMap = new HashMap<>();

        List<Integer> xBLackList = new ArrayList<>();
        List<Integer> yBlackList = new ArrayList<>();
        List<Integer> xList = new ArrayList<>();
        List<Integer> yList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int num = sc.nextInt();
                if (num == 0) {
                    xBLackList.add(i);
                    yBlackList.add(j);
                } else {
                    xList.add(i);
                    yList.add(j);
                }
            }
        }
        sc.close();
        if (xBLackList.size() == 0) {
            System.out.println(-1);
        } else {
            int min = Integer.MAX_VALUE, xSum, ySum , x, y;
            for (int i = 0; i < xList.size(); i++) {
                xSum = 0;
                ySum = 0;
                x = xBLackList.get(i);
                y = yBlackList.get(i);
                if (xMap.containsKey(x))
                    xSum = xMap.get(x);
                else {
                    for (int j = 0; j < xList.size(); j++) {
                        xSum += Math.abs(x - xList.get(j));
                    }
                    xMap.put(x, xSum);
                }
                if (yMap.containsKey(y))
                    ySum = yMap.get(y);
                else {
                    for (int j = 0; j < yList.size(); j++) {
                        ySum += Math.abs(y - yList.get(j));
                    }
                    yMap.put(y, ySum);
                }
                min = Math.min(min, xSum + ySum);
            }
        }
    }
}
